<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.hahehiho.myalbum.MyAlbumVO"%>
<%@page import="com.hahehiho.myalbum.MyAlbumDB"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0,width=device-width" />
<title>MyAlbum</title>
<link rel="stylesheet" href="css/default.css" >

</head>
<%
String userAgent = request.getHeader("user-agent");
int tdsize = 1;
if(userAgent.matches(".*BlackBerry.*")) {
} else if(userAgent.matches(".*Android.*")) {
} else if(userAgent.matches(".*iPhone.*")) {
} else if(userAgent.matches(".*iPad.*")) {
	tdsize = 2;
} else {
	tdsize = 4;
}

String depth = request.getParameter("depth");
String year = request.getParameter("year");
String month = request.getParameter("month");
String day = request.getParameter("day");
String isGroup = request.getParameter("isgroup");
if(isGroup == null) {
	isGroup = "true";
}
if(depth == null) {
	depth = "year";
}

MyAlbumDB db = MyAlbumDB.getInstance();
MyAlbumVO[] vos = db.selectPhotoList(depth, year, month, day, isGroup);

String groupLinkString = "";
String groupLinkDesc = "show all files";
String willIsGroup = "false";

if(isGroup.equalsIgnoreCase("false") ) {
	groupLinkDesc = "show group";
	willIsGroup = "true";
}

String header = "<a href=\"index.html\">HOME</a> | <a href=\"viewtime.jsp?depth=year&isgroup="+isGroup+"\">YEAR</a>";
if(year != null && !year.isEmpty()) {
	String link = "viewtime.jsp?depth=month&isgroup="+isGroup+"&year=" + year;
	header += " : <a href=\""+link+"\">" + year + "</a>";
}
if(month != null && !month.isEmpty()) {
	String link = "viewtime.jsp?depth=day&isgroup="+isGroup+"&year=" + year + "&month=" + month;
	header += " > <a href=\""+link+"\">" + month + "</a>";
}
if(day != null && !day.isEmpty()) {
	String link = "viewtime.jsp?depth=detail&isgroup="+isGroup+"&year=" + year + "&month=" + month + "&day=" + day;
	header += " >  <a href=\""+link+"\">" + day + "</a>";
}

groupLinkString = "viewtime.jsp?isgroup="+willIsGroup;	
if(depth.equalsIgnoreCase("year")) {
	groupLinkString += "&depth=year";
} else if(depth.equalsIgnoreCase("month")) {
	groupLinkString += "&depth=month&year="+year;
} else if(depth.equalsIgnoreCase("day")) {
	groupLinkString += "&depth=day&year="+year+"&month="+month;
} else if(depth.equalsIgnoreCase("detail")) {
	groupLinkString += "&depth=detail&year="+year+"&month="+month+"&day="+day;
}
%>
<body>
<table style="width:100%"><tr><td align="left" width="90%">
<h3><%=header%></h3>
</td>
<td width="10%" align="right"><a href="<%=groupLinkString%>"><%=groupLinkDesc%></a></td>
</tr></table>
<hr>
<table style="width:95%">
<%
int trindex = 0;


for(int i = 0; i < vos.length; i++) {
	if((i%tdsize) == 0) {
		trindex = i;
%>
<tr>
<%
	}
%>
<td align=center>
<%
	String linkString = "";
	String descString = "";
	
	String countString = "";
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
	
	countString = "(" + vos[i].getCount() + ")";
	if(isGroup.equalsIgnoreCase("true")) {
		if(depth.equalsIgnoreCase("year")) {
			linkString = "viewtime.jsp?depth=month&year=" + vos[i].getOrgDateYear();
			descString = "" + vos[i].getOrgDateYear();
		} else if (depth.equalsIgnoreCase("month")) {
			linkString = "viewtime.jsp?depth=day&year=" + vos[i].getOrgDateYear() + "&month=" + vos[i].getOrgDateMonth();
			descString = vos[i].getOrgDateYear() + "." + vos[i].getOrgDateMonth();
		} else if (depth.equalsIgnoreCase("day")) {
			linkString = "viewtime.jsp?depth=detail&year=" + vos[i].getOrgDateYear() + "&month=" + vos[i].getOrgDateMonth() + "&day=" + vos[i].getOrgDateDay();
			descString = vos[i].getOrgDateYear() + "." + vos[i].getOrgDateMonth() + "." + vos[i].getOrgDateDay();
		} else {
			linkString = "viewpic.jsp?pkey="+vos[i].getpKey();
			descString = sdf.format(vos[i].getOrgDate().getTime());
			countString = "";
		}
	} else {
		linkString = vos[i].getUrlFile();
		descString = sdf.format(vos[i].getOrgDate().getTime());
		countString = "";
	}
%>
<a href="<%=linkString%>"><img src=<%=vos[i].getThumbNailFile() %>><br><%=descString%> <%=countString%></a>
</td>
<%	
	if((i == (trindex+tdsize)) || (i == (vos.length-1))) {
%>
</tr>
<%
	}
}
%>
</table>
</body>
</html>